#!/bin/bash

# Install Dependencies Script for YOLOv8 OpenVINO Project
# Usage: ./install-dependencies.sh <host> <user> <password>

set -e

REMOTE_HOST="${1:-192.168.10.11}"
REMOTE_USER="${2:-asr}"
REMOTE_PASSWORD="${3:-123456}"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

# Check sshpass
if ! command -v sshpass &> /dev/null; then
    log_error "sshpass not installed"
    exit 1
fi

# SSH wrapper
ssh_exec() {
    sshpass -p "$REMOTE_PASSWORD" ssh -o StrictHostKeyChecking=no \
        "${REMOTE_USER}@${REMOTE_HOST}" "$1"
}

log_info "Installing dependencies on ${REMOTE_USER}@${REMOTE_HOST}..."
echo ""

# Update package manager
log_info "Updating package manager..."
ssh_exec "sudo apt-get update" > /dev/null 2>&1 || true

# Install build tools
log_info "Installing build tools..."
ssh_exec "sudo apt-get install -y build-essential cmake git wget curl" > /dev/null 2>&1

# Install OpenCV
log_info "Installing OpenCV..."
ssh_exec "sudo apt-get install -y libopencv-dev python3-opencv" > /dev/null 2>&1
log_success "OpenCV installed"

# Install nlohmann_json
log_info "Installing nlohmann_json..."
ssh_exec "sudo apt-get install -y nlohmann-json3-dev" > /dev/null 2>&1
log_success "nlohmann_json installed"

# Install OpenVINO
log_info "Installing OpenVINO..."
ssh_exec "sudo apt-get install -y openvino-dev" > /dev/null 2>&1
log_success "OpenVINO installed"

# Verify installations
echo ""
log_info "Verifying installations..."

log_info "Checking OpenCV..."
if ssh_exec "pkg-config --modversion opencv4" > /dev/null 2>&1; then
    version=$(ssh_exec "pkg-config --modversion opencv4")
    log_success "OpenCV 4: $version"
else
    log_error "OpenCV verification failed"
fi

log_info "Checking OpenVINO..."
if ssh_exec "test -d /opt/intel/openvino" > /dev/null 2>&1; then
    log_success "OpenVINO: Found"
else
    log_error "OpenVINO verification failed"
fi

log_info "Checking nlohmann_json..."
if ssh_exec "pkg-config --modversion nlohmann_json" > /dev/null 2>&1; then
    version=$(ssh_exec "pkg-config --modversion nlohmann_json")
    log_success "nlohmann_json: $version"
else
    log_error "nlohmann_json verification failed"
fi

echo ""
log_success "Dependency installation completed!"

